<template>
  <div class="app-container">
    <div class="nav">
      <h5>管理团队</h5>
      <el-button class="btn-add" type="success" @click="dialogFormVisible = true" size="mini">新增成员</el-button>
    </div>
    <div class="table-container">
      <el-table ref="productTable" :data="list" style="width: 100%" @selection-change="handleSelectionChange" v-loading="listLoading" border="">
        <el-table-column label="成员角色" align="center">
          <template slot-scope="scope">{{scope.row.roleName}}</template>
        </el-table-column>
        <el-table-column label="成员姓名" align="center">
          <template slot-scope="scope">
            <p>{{scope.row.name}}</p>
          </template>
        </el-table-column>
        <el-table-column label="成员手机号" align="center">
          <template slot-scope="scope">{{scope.row.mobile}}</template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <p>
              <el-button size="mini" @click="handleShowProduct(scope.$index, scope.row)">编辑</el-button>
              <el-button size="mini" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
            </p>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="pagination-container">
      <el-pagination background="" @size-change="handleSizeChange" @current-change="handleCurrentChange" layout="total, sizes,prev, pager, next,jumper" :page-size="listQuery.pageSize" :page-sizes="[5,10,15]" :current-page.sync="listQuery.pageNum" :total="total"></el-pagination>
    </div>
    <el-dialog title="编辑" :visible.sync="editSkuInfo.dialogVisible" width="40%">
      <el-form :model="agentVO">
        <el-form-item label="成员角色" :label-width="formLabelWidth">
          <el-select v-model="agentVO.role" @change="handleBrandChange" placeholder="成员角色">
            <el-option v-for="item in RoleList" :key="item.roleId" :label="item.roleName" :value="item.roleId"></el-option>
          </el-select>
          <!-- <el-input v-model="agentVO.roleName" autocomplete="off"></el-input> -->
        </el-form-item>
        <el-form-item label="成员姓名" :label-width="formLabelWidth">
          <el-input v-model="agentVO.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="成员手机号" :label-width="formLabelWidth">
          <el-input v-model="agentVO.mobile" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editSkuInfo.dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleEditSkuConfirm()">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="新增成员" :visible.sync="dialogFormVisible" style="width:80%;margin:0 auto">
      <el-form :model="agentVO">
        <el-form-item label="成员角色" :label-width="formLabelWidth">
          <el-select v-model="agentVO.role" @change="handleBrandChange()" placeholder="成员角色">
            <el-option v-for="item in RoleList" :key="item.roleId" :label="item.roleName" :value="item.roleId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="成员姓名" :label-width="formLabelWidth">
          <el-input v-model="agentVO.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="成员手机号" :label-width="formLabelWidth">
          <el-input v-model="agentVO.mobile" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="headleAddConfirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  fetchList,
  updateDeleteStatus,
  update,
  AddNewmember,
  roleList
} from "@/api/permission";

// import { fetchListWithChildren } from "@/api/productCate";

const defaultListQuery = {
  pageNum: 1,
  pageSize: 5,
  merchantId: 1
};
const defaultAgentVO = {
  merchantId: 1,
  name: null,
  mobile: null,
  roleId: null,
  roleName: null,
  role: null
};
export default {
  name: "productList",
  data() {
    return {
      dialogFormVisible: false,
      agentVO: Object.assign({}, defaultAgentVO),
      isEdit: false,
      RoleList: null,
      editSkuInfo: {
        dialogVisible: false
      },
      form: {
        merchantId: 1,
        name: null,
        mobile: null,
        roleId: null,
        roleName: null,
        role: null
      },
      formLabelWidth: "120px",
      operates: [
        {
          label: "商品上架",
          value: "publishOn"
        },
        {
          label: "商品下架",
          value: "publishOff"
        },
        {
          label: "设为推荐",
          value: "recommendOn"
        },
        {
          label: "取消推荐",
          value: "recommendOff"
        },
        {
          label: "设为新品",
          value: "newOn"
        },
        {
          label: "取消新品",
          value: "newOff"
        },
        {
          label: "转移到分类",
          value: "transferCategory"
        },
        {
          label: "移入回收站",
          value: "recycle"
        }
      ],
      operateType: null,
      listQuery: Object.assign({}, defaultListQuery),
      list: null,
      total: null,
      listLoading: true,
      selectProductCateValue: null,
      multipleSelection: [],
      productCateOptions: [],
      brandOptions: [],
      publishStatusOptions: [
        {
          value: 1,
          label: "上架"
        },
        {
          value: 0,
          label: "下架"
        }
      ],
      verifyStatusOptions: [
        {
          value: 1,
          label: "审核通过"
        },
        {
          value: 0,
          label: "未审核"
        }
      ]
    };
  },
  created() {
    this.getList();
    this.getRoleList();
    // this.getBrandList();
    // this.getProductCateList();
  },
  watch: {
    selectProductCateValue: function(newValue) {
      if (newValue != null && newValue.length == 2) {
        this.listQuery.productCategoryId = newValue[1];
      } else {
        this.listQuery.productCategoryId = null;
      }
    },
    checkedValue: function() {
      this.answer = [];
      this.answer.push(this.checkedValue);
    }
  },

  filters: {
    verifyStatusFilter(value) {
      if (value === 1) {
        return "审核通过";
      } else {
        return "未审核";
      }
    }
  },
  methods: {
    getProductSkuSp(row, index) {
      if (index === 0) {
        return row.sp1;
      } else if (index === 1) {
        return row.sp2;
      } else {
        return row.sp3;
      }
    },
    getList() {
      this.listLoading = true;
      fetchList(this.listQuery).then(response => {
        this.listLoading = false;
        this.list = response.data.list;
        this.total = response.data.total;
      });
    },
    getBrandList() {
      fetchBrandList({ pageNum: 1, pageSize: 100 }).then(response => {
        this.brandOptions = [];
        let brandList = response.data.list;
        for (let i = 0; i < brandList.length; i++) {
          this.brandOptions.push({
            label: brandList[i].name,
            value: brandList[i].id
          });
        }
      });
    },
    getRoleList() {
      roleList().then(response => {
        console.log(response);
        if (response.code == 0) {
          if (response.data) {
            this.RoleList = response.data;
          }
        } else {
        }
      });
    },
    handleBrandChange() {},
    // getProductCateList() {
    //   fetchListWithChildren().then(response => {
    //     let list = response.data;
    //     this.productCateOptions = [];
    //     for (let i = 0; i < list.length; i++) {
    //       let children = [];
    //       if (list[i].children != null && list[i].children.length > 0) {
    //         for (let j = 0; j < list[i].children.length; j++) {
    //           children.push({
    //             label: list[i].children[j].name,
    //             value: list[i].children[j].id
    //           });
    //         }
    //       }
    //       this.productCateOptions.push({
    //         label: list[i].name,
    //         value: list[i].id,
    //         children: children
    //       });
    //     }
    //   });
    // },
    handleEditSkuConfirm() {
      this.$confirm("是否要进行修改", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.editSkuInfo.dialogVisible = false;
        update(this.agentVO).then(response => {
          this.$message({
            message: "修改成功",
            type: "success",
            duration: 1000
          });
          this.getList();
        });
      });
    },
    headleAddConfirm() {
      this.$confirm("是否要进行修改", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        let params = new URLSearchParams();
        params.append("merchantId ", this.agentVO.merchantId);
        params.append("name", this.agentVO.name);
        params.append("mobile", this.agentVO.mobile);
        params.append("roleId", this.agentVO.role);
        params.append("roleName", this.agentVO.role.roleName);
        AddNewmember(params).then(response => {
          this.$message({
            message: "新增成功",
            type: "success",
            duration: 1000
          });
          this.dialogFormVisible = false;
          this.getList();
        });
      });
    },

    handleBatchOperate() {
      if (this.operateType == null) {
        this.$message({
          message: "请选择操作类型",
          type: "warning",
          duration: 1000
        });
        return;
      }
      if (this.multipleSelection == null || this.multipleSelection.length < 1) {
        this.$message({
          message: "请选择要操作的商品",
          type: "warning",
          duration: 1000
        });
        return;
      }
      this.$confirm("是否要进行该批量操作?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        let ids = [];
        for (let i = 0; i < this.multipleSelection.length; i++) {
          ids.push(this.multipleSelection[i].id);
        }
        switch (this.operateType) {
          case this.operates[0].value:
            this.updatePublishStatus(1, ids);
            break;
          case this.operates[1].value:
            this.updatePublishStatus(0, ids);
            break;
          case this.operates[2].value:
            this.updateRecommendStatus(1, ids);
            break;
          case this.operates[3].value:
            this.updateRecommendStatus(0, ids);
            break;
          case this.operates[4].value:
            this.updateNewStatus(1, ids);
            break;
          case this.operates[5].value:
            this.updateNewStatus(0, ids);
            break;
          case this.operates[6].value:
            break;
          case this.operates[7].value:
            this.updateDeleteStatus(1, ids);
            break;
          default:
            break;
        }
        this.getList();
      });
    },
    handleSizeChange(val) {
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listQuery.pageNum = val;
      this.getList();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handlePublishStatusChange(index, row) {
      let ids = [];
      ids.push(row.id);
      this.updatePublishStatus(row.publishStatus, ids);
    },
    handleNewStatusChange(index, row) {
      let ids = [];
      ids.push(row.id);
      this.updateNewStatus(row.newStatus, ids);
    },
    handleRecommendStatusChange(index, row) {
      let ids = [];
      ids.push(row.id);
      this.updateRecommendStatus(row.recommandStatus, ids);
    },
    handleResetSearch() {
      this.selectProductCateValue = [];
      this.listQuery = Object.assign({}, defaultListQuery);
    },
    handleDelete(index, row) {
      this.$confirm("是否要进行删除操作?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        let params = new URLSearchParams();
        params.append("userId", row.userId);
        updateDeleteStatus(params).then(response => {
          this.$message({
            message: "删除成功",
            type: "success",
            duration: 1000
          });
          this.getList();
        });
      });
    },

    handleShowProduct(index, row) {
      this.editSkuInfo.dialogVisible = true;
      this.isEdit = true;
      this.agentVO = Object.assign({}, row);
    },
    handleShowVerifyDetail(index, row) {
      console.log("handleShowVerifyDetail", row);
    },
    handleShowLog(index, row) {
      console.log("handleShowLog", row);
    },
    updatePublishStatus(publishStatus, ids) {
      let params = new URLSearchParams();
      params.append("ids", ids);
      params.append("publishStatus", publishStatus);
      updatePublishStatus(params).then(response => {
        this.$message({
          message: "修改成功",
          type: "success",
          duration: 1000
        });
      });
    },
    updateNewStatus(newStatus, ids) {
      let params = new URLSearchParams();
      params.append("ids", ids);
      params.append("newStatus", newStatus);
      updateNewStatus(params).then(response => {
        this.$message({
          message: "修改成功",
          type: "success",
          duration: 1000
        });
      });
    }
    // updateRecommendStatus(recommendStatus, ids) {
    //   let params = new URLSearchParams();
    //   params.append("ids", ids);
    //   params.append("recommendStatus", recommendStatus);
    //   updateRecommendStatus(params).then(response => {
    //     this.$message({
    //       message: "修改成功",
    //       type: "success",
    //       duration: 1000
    //     });
    //   });
    // },
    // updateDeleteStatus( ids) {
    //   let params = new URLSearchParams();
    //   params.append("userId ", ids);
    //   updateDeleteStatus(params).then(response => {
    //     this.$message({
    //       message: "删除成功",
    //       type: "success",
    //       duration: 1000
    //     });
    //   });
    //   this.getList();
    // }
  }
};
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.app-container {
  background: #fff;
  min-height: 500px;
  .nav {
    margin-bottom: 10px;
    display: flex;
    justify-content: space-between;
    h5 {
      font-size: 16px;
    }
    .btn-add {
      height: 48px;
    }
  }
}
</style>


